package com.metagurukul.metagranth.actionservlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.metagurukul.metagranth.exception.MetaSqlException;
import com.metagurukul.metagranth.service.QuestionService;
import com.metagurukul.metagranth.service.TagService;
import com.metagurukul.metagranth.vo.Question;
import com.metagurukul.metagranth.vo.Tag;

/**
 * This Servlet is responsible for handling the functionality of gettinf details of Questions that are to be Updated
 * Gets the questionId from the jsp page, retrieves the questionDetails by that questionId .
 * Also get the tags associated with that question by questionId.
 * @author team MetaGranth
 *
 */
public class GetQuestionToBeUpdatedServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public GetQuestionToBeUpdatedServlet() {
        super();

    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * this function gets the questionId of the  question which are to be updated
	 * Returns the questionObject associated with that questionId by calling getQuestionById() function
	 * in the QuestionService layer.
	 * It also finds the tags associated with that questionId in tags and dispatches both the
	 * questionObj and tags to the editingQuestion.jsp page.
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doPerform(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		if(session.getAttribute("userId")!=null){	
		try {
			//gets the questionId of a particular question
			int questionId=Integer.parseInt(request.getParameter("questionId"));
			//gets the questions details in question Object by calling getQuestionById()function in the QuestionService layer
			Question questionObj = QuestionService.getQuestionById(questionId);
			ArrayList<Tag> tags=null;
		
			tags = TagService.getTagsByQuestionId(questionId);
			request.setAttribute("questionToBeUpdated", questionObj);
			request.setAttribute("tagsToBeUpdated", tags);
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("editingQuestion.jsp");
			requestDispatcher.forward(request, response);
		} catch (MetaSqlException exception) {
			request.setAttribute("errormsg", exception.getMessage());
			request.getRequestDispatcher("errorPage.jsp").forward(request, response);
			
		}
		}else {
			response.sendRedirect("index.jsp");
		}
	}
	
}
